package com.woniu.ai;

import com.alibaba.fastjson.JSONObject;
import okhttp3.*;

import java.util.Map;
import java.util.Scanner;

public class WoniuAi {

    public static String doPost(String url, String json) {
        // 构建请求数据
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        Request.Builder requestbuilder = new Request.Builder()
                .url(url)
                .post(RequestBody.create(mediaType, json));

        Request request = requestbuilder.build();
        OkHttpClient okHttpClient = new OkHttpClient();
        try {
            Response response = okHttpClient.newCall(request).execute();
            return response.body().string();
        }
        catch (Exception e) {
            return "Request Failed";
        }
    }

    public static void main(String[] args) {
        String url="http://localhost:11434/api/generate";
        while (true){
            System.out.println("请输入你要问的问题");
            Scanner scanner = new Scanner(System.in);
            String message= scanner.next();
            String json="{'model': 'deepseek-r1:1.5b','prompt': '%s', 'stream': false}"
                    .formatted(message).replace("'", "\"");
            String result = doPost(url, json);
            Map map = JSONObject.parseObject(result, Map.class);
            System.out.println(map.get("response"));
//            System.out.println("输入y继续");
//            String a=scanner.next();
//            if (a.equals("y")){
//            }else{
//                break;
//            }
        }
    }
}
